package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.dto.XeVenCarDTO;
import com.xebest.api.dto.XeVenCarResDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.dto.BaseResponseDTO;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.constant.XeErrorCode;
import com.xebest.web.model.XeVenCarEntity;
import com.xebest.web.service.XeLogisticsCarService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 物流车辆管理
 *
 * @author xzd
 */
@RestController
@RequestMapping("restapi/logisticsCar")
public class XeLogisticsCarRestApi extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeLogisticsCarRestApi.class);
    @Autowired
    private XeLogisticsCarService xeLogisticsCarService;

    /**
     * 给鲜易商城提供的车辆信息列表接口
     *
     * @author xzd
     */
    @RequestMapping("logisticsCarInfoList")
    public XeVenCarResDTO logisticsCarInfoList(@RequestBody XeVenCarDTO xeVenCarDTO) {
        XeVenCarResDTO responseDTO = new XeVenCarResDTO();
        responseDTO.setMsg("查询物流车辆列表");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        String paramJson = JSONObject.toJSONString(xeVenCarDTO);
        log.info("物流车辆管理列表,传入参数： {}", paramJson);
        if (xeVenCarDTO.getCarNumORmobile() == null) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        XeVenCarEntity xeVenCarEntity = new XeVenCarEntity();
        xeVenCarEntity.setCarNumORmobile(xeVenCarDTO.getCarNumORmobile().toUpperCase());
        // 分页查询
        PageHelper.startPage(xeVenCarDTO.getXePageDto().getPageNum(), xeVenCarDTO.getXePageDto().getPageSize());
        try {
            List<XeVenCarEntity> list = xeLogisticsCarService.logisticsCarInfoList(xeVenCarEntity);
            List<XeVenCarDTO> listDTO = JSONArray.parseArray(JSONObject.toJSONString(list), XeVenCarDTO.class);
            // 分页信息
            XePageInfoDto<XeVenCarEntity> xePageInfoEntity = new XePageInfoDto<XeVenCarEntity>(list);
            XePageInfoDto<XeVenCarDTO> xePageInfoDto = JSONObject.parseObject(JSONObject.toJSONString(xePageInfoEntity), XePageInfoDto.class);
            responseDTO.setXePageInfoDto(xePageInfoDto);
            responseDTO.setVenCarList(listDTO);
            responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.info("物流车辆管理列表异常", e);
            responseDTO.setMsg("查询物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }

    /**
     * 给鲜易商城提供的车辆详情接口
     *
     * @author xzd
     */
    @RequestMapping("logisticsCarInfoDetail")
    public XeVenCarResDTO logisticsCarInfoDetail(@RequestBody XeVenCarDTO xeVenCarDTO) {
        XeVenCarResDTO responseDTO = new XeVenCarResDTO();
        responseDTO.setMsg("查询物流车辆详情失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        String paramJson = JSONObject.toJSONString(xeVenCarDTO);
        log.info("查询物流车辆详情,传入参数： {}", paramJson);
        if (xeVenCarDTO.getCarId() == null) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        try {
            XeVenCarEntity carEntity = xeLogisticsCarService.logisticsCarDetailByCarId(xeVenCarDTO.getCarId());
            xeVenCarDTO = JSONObject.parseObject(JSONObject.toJSONString(carEntity), XeVenCarDTO.class);
            responseDTO.setXeVenCarDTO(xeVenCarDTO);
            responseDTO.setMsg("查询物流车辆详情成功");
            responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.info("查询物流车辆详情异常", e);
            responseDTO.setMsg("查询物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }


    /**
     * 物流车辆管理列表
     *
     * @author xzd
     */
    @RequestMapping("queryLogisticsCarList")
    public XeVenCarResDTO queryLogisticsCarList(@RequestBody XeVenCarDTO xeVenCarDTO) {
        XeVenCarResDTO responseDTO = new XeVenCarResDTO();
        responseDTO.setMsg("查询物流车辆列表");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        String paramJson = JSONObject.toJSONString(xeVenCarDTO);
        log.info("物流车辆管理列表,传入参数： {}", paramJson);
        // 1、验证参数

        // 2、拼装参数,
        XeVenCarEntity xeVenCarEntity = JSONObject.parseObject(paramJson, XeVenCarEntity.class);
        // 分页查询
        PageHelper.startPage(xeVenCarDTO.getXePageDto().getPageNum(), xeVenCarDTO.getXePageDto().getPageSize());
        // 3、返回结果
        try {
            List<XeVenCarEntity> list = xeLogisticsCarService.queryLogisticsCarList(xeVenCarEntity);
            List<XeVenCarDTO> listDTO = JSONArray.parseArray(JSONObject.toJSONString(list), XeVenCarDTO.class);
            // 分页信息
            XePageInfoDto<XeVenCarEntity> xePageInfoEntity = new XePageInfoDto<XeVenCarEntity>(list);
            XePageInfoDto<XeVenCarDTO> xePageInfoDto = JSONObject.parseObject(JSONObject.toJSONString(xePageInfoEntity), XePageInfoDto.class);
            responseDTO.setXePageInfoDto(xePageInfoDto);
            responseDTO.setVenCarList(listDTO);
            responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.info("物流车辆管理列表异常", e);
            responseDTO.setMsg("查询物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }


    /**
     * 物流车辆详情
     *
     * @author xzd
     */
    @RequestMapping("queryLogisticsCarByCarId")
    public XeVenCarResDTO queryLogisticsCarByCarId(@RequestBody XeVenCarDTO xeVenCarDTO) {
        XeVenCarResDTO responseDTO = new XeVenCarResDTO();
        responseDTO.setMsg("查询物流车辆详情失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        String paramJson = JSONObject.toJSONString(xeVenCarDTO);
        log.info("物流车辆管理列表,传入参数： {}", paramJson);
        if (xeVenCarDTO.getCarId() == null) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        try {
            Map<String, Object> reMap = xeLogisticsCarService.queryLogisticsCarByCarId(xeVenCarDTO.getCarId(), xeVenCarDTO.getXePageDto().getPageNum(), xeVenCarDTO.getXePageDto().getPageSize());
            xeVenCarDTO = JSONObject.parseObject(JSONObject.toJSONString(reMap.get("xeVenCarEntity")), XeVenCarDTO.class);
            XePageInfoDto<XeVenCarDTO> xePageInfoDto = JSONObject.parseObject(JSONObject.toJSONString(reMap.get("xePageInfo")), XePageInfoDto.class);
            responseDTO.setXePageInfoDto(xePageInfoDto);
            responseDTO.setXeVenCarDTO(xeVenCarDTO);
            responseDTO.setMsg("查询物流车辆详情成功");
            responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.info("查询物流车辆详情异常", e);
            responseDTO.setMsg("查询物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }

    /**
     * 车辆服务商户
     *
     * @author xzd
     */
    @RequestMapping("venDetail")
    public XeVenCarResDTO venDetail(@RequestBody XeVenCarDTO xeVenCarDTO) {
        XeVenCarResDTO responseDTO = new XeVenCarResDTO();
        responseDTO.setMsg("查询车辆服务商户失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        String paramJson = JSONObject.toJSONString(xeVenCarDTO);
        log.info("物流车辆服务商户,传入参数： {}", paramJson);
        if (xeVenCarDTO.getCarId() == null) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        // 分页查询
        PageHelper.startPage(xeVenCarDTO.getXePageDto().getPageNum(), xeVenCarDTO.getXePageDto().getPageSize());
        XeVenCarEntity xeVenCarEntity = new XeVenCarEntity();
        xeVenCarEntity.setCarId(xeVenCarDTO.getCarId());
        try {
            List<XeVenCarEntity> list = xeLogisticsCarService.venDetail(xeVenCarEntity);

            List<XeVenCarDTO> listDTO = JSONArray.parseArray(JSONObject.toJSONString(list), XeVenCarDTO.class);
            // 分页信息
            XePageInfoDto<XeVenCarEntity> xePageInfoEntity = new XePageInfoDto<XeVenCarEntity>(list);
            XePageInfoDto<XeVenCarDTO> xePageInfoDto = JSONObject.parseObject(JSONObject.toJSONString(xePageInfoEntity), XePageInfoDto.class);
            responseDTO.setXePageInfoDto(xePageInfoDto);
            responseDTO.setVenCarList(listDTO);
            responseDTO.setMsg("查询车辆服务商户成功");
            responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);

        } catch (Exception e) {
            log.info("查询物流车辆服务商户异常", e);
            responseDTO.setMsg("询物流车辆服务商户异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }

    /**
     * 新增物流车辆
     *
     * @author xzd
     */
    @RequestMapping("insertLogisticsCar")
    public BaseResponseDTO insertLogisticsCar(@RequestBody XeVenCarDTO xeVenCarDTO) {
        log.info("新增物流车辆传入参数：" + JSONObject.toJSONString(xeVenCarDTO));
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        responseDTO.setMsg("新增物流车辆失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        if (StringUtils.isBlank(xeVenCarDTO.getCarPlateNum()) || StringUtils.isBlank(xeVenCarDTO.getMobilePhone())
                || (xeVenCarDTO.getListLine() == null || xeVenCarDTO.getListLine().size() == 0)) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }

        XeVenCarEntity carEntity = JSONObject.parseObject(JSONObject.toJSONString(xeVenCarDTO), XeVenCarEntity.class);
        carEntity.setCarPlateNum(xeVenCarDTO.getCarPlateNum().toUpperCase());
        try {
            int num = xeLogisticsCarService.insertLogisticsCar(carEntity);
            if (num > 0) {
                initLogisticsCar(carEntity);
                responseDTO.setMsg("新增物流车辆成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("新增物流车辆异常", e);
            responseDTO.setMsg("新增物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
        }
        return responseDTO;
    }


    /**
     * 修改物流车辆
     *
     * @author xzd
     */
    @RequestMapping("updateLogisticsCar")
    public BaseResponseDTO updateLogisticsCar(@RequestBody XeVenCarDTO xeVenCarDTO) {
        log.info("修改车辆路线传入参数：" + JSONObject.toJSONString(xeVenCarDTO));
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        responseDTO.setMsg("修改车辆失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        if (StringUtils.isBlank(xeVenCarDTO.getCarPlateNum()) || StringUtils.isBlank(xeVenCarDTO.getMobilePhone())
                || xeVenCarDTO.getCarId() == null || (xeVenCarDTO.getListLine() == null || xeVenCarDTO.getListLine().size() == 0)) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        XeVenCarEntity carEntity = JSONObject.parseObject(JSONObject.toJSONString(xeVenCarDTO), XeVenCarEntity.class);
        carEntity.setCarPlateNum(xeVenCarDTO.getCarPlateNum().toUpperCase());
        try {
            int num = xeLogisticsCarService.updateLogisticsCar(carEntity);
            if (num > 0) {
                initLogisticsCar(carEntity);
                responseDTO.setMsg("修改成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("修改物流车辆异常", e);
            responseDTO.setMsg("修改物流车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
        }
        return responseDTO;
    }

    /**
     * 删除车辆
     *
     * @author xzd
     */
    @RequestMapping("delLogisticsCar")
    public BaseResponseDTO delLogisticsCar(@RequestBody XeVenCarDTO xeVenCarDTO) {
        log.info(" 删除车辆传入参数：" + JSONObject.toJSONString(xeVenCarDTO));
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        responseDTO.setMsg("删除车辆失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        if (xeVenCarDTO.getCarId() == null) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        try {
            int num = xeLogisticsCarService.delLogisticsCar(xeVenCarDTO.getCarId());
            if (num > 0) {
                XeVenCarEntity xeVenCarEntity = xeLogisticsCarService.logisticsCarDetailByCarId(xeVenCarDTO.getCarId());
                initLogisticsCar(xeVenCarEntity);
                responseDTO.setMsg("删除车辆成功");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            }
        } catch (Exception e) {
            log.info("删除车辆异常", e);
            responseDTO.setMsg("删除车辆异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
        }
        return responseDTO;
    }

    /**
     * 校验车牌号是否重复
     *
     * @author xzd
     */
    @RequestMapping("checkCarNum")
    public BaseResponseDTO checkCarNum(@RequestBody XeVenCarDTO xeVenCarDTO) {
        log.info(" 校验车牌号是否重复：" + JSONObject.toJSONString(xeVenCarDTO));
        BaseResponseDTO responseDTO = new BaseResponseDTO();
        responseDTO.setMsg("校验车牌号是否重复失败");
        responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        if (StringUtils.isBlank(xeVenCarDTO.getCarPlateNum())) {
            responseDTO.setMsg("缺少必要参数");
            responseDTO.setStatus(XeErrorCode.PORJECT_PARAM_ERROR);
            return responseDTO;
        }
        try {
            int num = xeLogisticsCarService.checkCarNum(xeVenCarDTO.getCarPlateNum());
            if (num == 0) {
                responseDTO.setMsg("该车牌号未在系统中，可以添加此车牌号");
                responseDTO.setStatus(XeErrorCode.PORJECT_SUCCESS);
            } else {
                responseDTO.setMsg("系统中已存在此车牌号");
                responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
            }

        } catch (Exception e) {
            log.info("校验车牌号异常", e);
            responseDTO.setMsg("校验车牌号异常");
            responseDTO.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return responseDTO;
    }


    /**
     * 初始化物流车辆
     *
     * @author xzd
     */
    public void initLogisticsCar(XeVenCarEntity xeVenCarEntity) {

        log.info("=============== 初始化物流车辆 =============== start");
        try {
            xeLogisticsCarService.initLogisticsCar(xeVenCarEntity);
        } catch (Exception e) {
            log.error("初始化物流车辆异常", e);
        }
    }

}
  
